import json
import os
from my_json import load_json, save_json

basic_path = str(os.path.dirname(__file__)) + "/"
map_path = basic_path + "../school_grade_sim/mapnew.json"
def get_map():
    id_name = load_json(basic_path + "dicname2id.json")
    choose_map = load_json(map_path)
    for root, dirs, files in os.walk(basic_path, topdown=True):
        for file in files:
            if "school_grade" in file and ".json" in file:
                file_path = os.path.join(root, file)
                d = {}
                print(file_path)
                with open(file_path, 'r', encoding='utf-8') as json_file:
                    json_data = json.load(json_file)
                    print(file_path)
                    for k1, v1 in json_data.items():  # school_id
                        if len(v1) == 0:
                            continue
                        for k2, v2 in v1.items():  # year
                            if len(v2) == 0:
                                continue
                            for k3, v3 in v2.items():  # type
                                if len(v3) == 0:
                                    continue
                                k4 = k3.split("_")[0]
                                if k4 == "1":
                                    continue
                                elif k4 == "2":
                                    continue
                                elif k4 == "3":
                                    v4 = v3["item"]
                                    if len(v4) == 0:
                                        continue
                                    for v5 in v4:
                                        if "sg_info" in v5 and v5["sg_info"] != "":
                                            if v5["sg_info"] not in d:
                                                d[v5["sg_info"]] = set()
                                            d[v5["sg_info"]].add(v5["special_group"])
                                            if v5["sg_info"] not in choose_map:
                                                l.add(v5["sg_info"])
                                                print("type error")
                                        elif "sp_info" in v5 and v5["sp_info"] != "":
                                            if v5["sp_info"] not in d:
                                                d[v5["sp_info"]] = set()
                                            d[v5["sp_info"]].add(v5["special_group"])
                                            if v5["sp_info"] not in choose_map:
                                                l.add(v5["sp_info"])
                                                print("type error")
                                        else:
                                            l.add(v5["special_group"])
                                            continue
                                else:
                                   print("特殊招生")
    for k in d.keys():
        d[k] = list(d[k])
    d["aaa"] = list(l)
    with open(basic_path + 'map.json', 'w', encoding='utf-8') as json_file:
        json.dump(d, json_file, ensure_ascii=False, indent=4)


get_map()


def get_map_help(item, type):
    d = {}
    try:
        max0 = float(item["max"])
    except:
        max0 = 0
    d["max"] = max0
    try:
        min0 = float(item["min"])
    except:
        min0 = 0
    d["min"] = min0
    try:
        avg0 = float(item["average"])
    except:
        avg0 = 0
    d["avg"] = avg0
    try:
        min_rank = int(item["min_section"])
    except:
        min_rank = 0
    d["min_rank"] = min_rank
    d["bach_name"] = item["local_bach_name"]
    if type == "1":
        d["choose"] = {'required': ["物理", "化学", "生物"], 'optional': [], "optional_num": 0}
    elif type == "2":
        d["choose"] = {'required': ["历史", "地理", "思想政治"], 'optional': [], "optional_num": 0}
    return d
